//
//  ZDMyVoucherViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/16.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "ZDMyVoucherViewController.h"
#import "AFHTTPSessionManager+POST.h"
#import "MJExtension.h"
#import "ZDMyVoucherTableViewCell.h"
#import "ZDMyVoucherModel.h"
#import "ZHRTRequestFailureShowViewOrDetail.h"
@interface ZDMyVoucherViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSArray *voucherArr;

@property (nonatomic, weak) UITableView *tableView;


@property (nonatomic, copy) NSString *defaultVoucherPng;
@end

@implementation ZDMyVoucherViewController

- (NSArray *)backArr {
    if (_voucherArr == nil) {
        _voucherArr = [NSArray array];
    }
    return _voucherArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];
    [self setupUI];
}

- (void)loadData {
    
    NSDictionary * param = @{@"method":@"myVoucherList",@"page":@"0"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            /*
             
             {"method":"myVoucherList","mode":"iOS","page":"0","token":"43e1b517cddf4ad38ffc0e334e517591","uid":29,"version":"1.0"}
             
             {"method":"myVoucherList","mode":"iOS","page":"0","token":"ab19d02f383241f3b10fb20334b6409e","uid":87,"version":"1.0"}
             
             
             */
            /*
             
             message = "\U64cd\U4f5c\U6210\U529f";
             result =     {
             defaultVoucherPng = "http://172.16.20.248:6060/zhrtChinaWeb//upload/2017/02/15/adcc711f-3693-443b-b31b-2af734282842/\U80cc\U666f1@3x.png";
             myVouchersPage =         (
             {
             denomination = 120;
             expirationTime = "2016-09-24 00:00:00";
             getTime = "2016-09-23 01:12:10";
             id = 111540;
             limitInvest = 20000;
             postPeriod = 1;
             status = 1;
             voucherStr = "\U73b0\U91d1\U5377";
             voucherType = 1;
             },
             {
             denomination = 4;
             expirationTime = "2016-09-24 00:00:00";
             getTime = "2016-09-23 02:18:33";
             id = 111616;
             limitInvest = 10000;
             postPeriod = 1;
             status = 1;
             voucherStr = "\U52a0\U606f\U52b5";
             voucherType = 2;
             },
             {
             denomination = 4;
             expirationTime = "2016-09-24 00:00:00";
             getTime = "2016-09-23 02:18:16";
             id = 111612;
             limitInvest = 10000;
             postPeriod = 1;
             status = 1;
             voucherStr = "\U52a0\U606f\U52b5";
             voucherType = 2;
             },
             {
             denomination = 120;
             expirationTime = "2016-09-24 00:00:01";
             getTime = "2016-09-23 02:18:46";
             id = 111624;
             limitInvest = 20000;
             postPeriod = 1;
             status = 1;
             voucherStr = "\U73b0\U91d1\U5377";
             voucherType = 1;
             }
             );
             totalPages = 10;
             };
             type = success;
             }
             
             */
         
            self.voucherArr = [ZDMyVoucherModel mj_objectArrayWithKeyValuesArray:responseObject[@"result"][@"myVouchersPage"]];
            if (self.voucherArr.count) {
                
                self.defaultVoucherPng = responseObject[@"result"][@"defaultVoucherPng"];
                [self.tableView reloadData];
            } else {
                
                [ZHRTRequestFailureShowViewOrDetail showNoDataForView:self.view imageNamed:@"noVoucher" text:@"暂无可用优惠券" type:1];
            }
            
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        } else {
            
            [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)navigationBarCof {
    self.title = @"我的优惠券";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:kColorRGB(187,147,115) ,NSFontAttributeName:kBaseFont_(19)}];
   [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"transparentNav"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self navigationBarCof];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)setupUI {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.backgroundColor = kColorRGB(234,234,234);
    [self.view addSubview:tableView];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView = tableView;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    tableView.tableFooterView = view;
    
}

#pragma mark -- table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.voucherArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *myID = @"ZDMyVoucherTableViewCell";
        ZDMyVoucherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        cell = [[ZDMyVoucherTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myID];
    }
    cell.voucherModel = self.voucherArr[indexPath.row];
    cell.defaultVoucherPng = self.defaultVoucherPng;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 173;
}

@end
